<template>
<div class="settings-page">
    <div class="container page">
        <div class="row">

        <div class="col-md-6 offset-md-3 col-xs-12">
            <h1 class="text-xs-center">Your Settings</h1>

            <form @submit.prevent="onUpdateSetting">
            <fieldset>
                <fieldset class="form-group">
                    <input :disabled="updating" v-model="user.image" class="form-control" type="text" placeholder="URL of profile picture">
                </fieldset>
                <fieldset class="form-group">
                    <input :disabled="updating" v-model="user.username" class="form-control form-control-lg" type="text" placeholder="Your Name" required>
                </fieldset>
                <fieldset class="form-group">
                    <textarea :disabled="updating" v-model="user.bio" class="form-control form-control-lg" rows="8" placeholder="Short bio about you"></textarea>
                </fieldset>
                <fieldset class="form-group">
                    <input :disabled="updating" v-model="user.email" class="form-control form-control-lg" type="email" placeholder="Email" required>
                </fieldset>
                <fieldset class="form-group">
                    <input :disabled="updating" v-model="user.password" class="form-control form-control-lg" type="password" placeholder="New Password">
                </fieldset>
                <button class="btn btn-lg btn-primary pull-xs-right" :disabled="updating">
                    Update Settings
                </button>
            </fieldset>
            </form>
            <hr>
            <!-- 登出 -->
            <button class="btn btn-outline-danger" @click="logout">
                Or click here to logout.
            </button>
        </div>

        </div>
    </div>
</div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { updateProfile } from '@/api/user'

const Cookie = process.client ? require('js-cookie') : undefined

export default {
    middleware: 'authenticated',
    name: 'SettingsIndex',
    data() {
        return {
            user: {
                image: '',
                username: '',
                bio: '',
                email: '',
                password: ''
            },
            updating: false
        }
    },
    mounted() {
        if (this.$store.state.user) {
            this.user.image = this.$store.state.user.image
            this.user.username = this.$store.state.user.username
            this.user.email = this.$store.state.user.email
            this.user.bio = this.$store.state.user.bio
        }
    },
    methods: {
        ...mapMutations(['setUser']),
        async onUpdateSetting() {
            this.updating = true
            const { data } =  await updateProfile(this.user) // 返回更新后的user 去更新store的数据并跳转页面
            this.setUser(data.user)
            Cookie.set('user', data.user)
            this.$router.push({name: 'profile', params: { username: data.user.username }})
            this.updating = false
        },
        logout() {
            this.setUser(null)
            Cookie.remove('user')
            this.$router.push({name: 'home'})
        }
    }
}
</script>

<style>

</style>